import pymysql


class DBUtilTools(object):
    @classmethod
    def __create_conn(cls):
        conn = pymysql.connect(host="211.103.136.244", port=7061, user="student",
                               password="iHRM_student_2021", database="ihrm", charset="utf8")
        return conn

    @classmethod
    def query_one(cls, sql):
        cursor = None
        conn = None
        res = None
        try:
            conn = DBUtilTools.__create_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
        except Exception as err:
            print("执行查询SQL失败:", str(err))
        finally:
            cursor.close()
            conn.close()
            return res

    @classmethod
    def db_uid(cls, sql):
        cursor = None
        conn = None
        try:
            conn = DBUtilTools.__create_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            print("Affected rows:", conn.affected_rows())
            conn.commit()
        except Exception as err:
            print("执行增删改SQL失败:", str(err))
            conn.rollback()
        finally:
            cursor.close()
            conn.close()


if __name__ == '__main__':
    DBUtilTools.db_uid("insert into bs_user (id,mobile,username) values('1212344234','13150306183','tank12345');")
    DBUtilTools.db_uid("delete from bs_user where mobile ='13150306183';")
